package com.zeng.webmvc.listener;

import com.zeng.webmvc.threadlocal.ConfigThreadLocal;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.ConfigProviderResolver;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;

/**
 * @author zyb
 * @date 2021-03-26 23:25
 */
public class ComponentRequestListener implements ServletRequestListener {


    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        ServletContext servletContext = sre.getServletContext();
        ClassLoader classLoader = servletContext.getClassLoader();
        ConfigProviderResolver configProviderResolver = ConfigProviderResolver.instance();
        Config config = configProviderResolver.getConfig(classLoader);
        ConfigThreadLocal.setConfig(config);
    }
    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        ConfigThreadLocal.remove();

    }
}
